package com.open.exam.itemmodel;

import com.open.exam.common.CommonFunction;
import com.open.exam.dto.Choices;
import com.open.exam.dto.ItemTemplate;
import com.openexam.openqti.openqtipaperquestion.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 奶茶
 * Created by on 2017/12/14.
 */
public class ICreaterFactory {

    public static IHtmlCreater getInstance(ItemTemplate template){
        return getCreater(CommonFunction.getItemType(template.getAssessmentItemType()));
    }

    private static IHtmlCreater getCreater(ItemType itemType){
        IHtmlCreater iHtmlCreater = null;

        switch (itemType){
            //完型填空
            case CLOZE:
                iHtmlCreater = new ItemClozeCreater();
                break;
            //问答题
            case EXTENDEDTEXT:
                iHtmlCreater = new ItemExtendedTextCreater();
                break;
            //判断题
            case JUDGEMENT:
                iHtmlCreater = new ItemJudgementCreater();
                break;
            //多选题
            case MULTIPLECHOICE:
                iHtmlCreater = new ItemMultipleChoiceCreater();
                break;
            //阅读理解题
            case READINGCOMPREHENSION:
                iHtmlCreater = new ItemReadingComprehensionCreater();
                break;
            //单选题
            case SINGLECHOICE:
                iHtmlCreater = new ItemSingleChoiceCreater();
                break;
            //填空题
            case TEXTENTRY:
                iHtmlCreater = new ItemTextEntryCreater();
                break;
            default:
                    break;
        }
        return iHtmlCreater;
    }

    /**
     * 加载试题引用，返回试题实例
     * @param openAssessmentItem
     * @return
     */
    public static ItemTemplate getItemObject(OpenAssessmentItem openAssessmentItem){

        String[] strIndex = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
        ItemTemplate itemTemplate = null;
        ItemType type = openAssessmentItem.getAssessmentItemType();
        switch (type){
            //完型填空
            case CLOZE:
                OpenClozeAssessmentItem clozeAssessmentItem = (OpenClozeAssessmentItem) openAssessmentItem;
                ItemTemplate temp = new ItemTemplate();
                temp.setOriginalValue(clozeAssessmentItem.getPrompt().getOriginalValue());
                List<ItemTemplate> subItems = new ArrayList<>();
                for (OpenSingleChoiceAssessmentItem sub:clozeAssessmentItem.getSubItems()) {
                    ItemTemplate subItemModel = new ItemTemplate();
                    subItemModel.setOriginalValue(sub.getPrompt().getOriginalValue());
                    List<Choices> subItemChoice = new ArrayList<>();
                    for (OpenSimpleChoiceAtomicItem choice:sub.getChoices()) {
                        Choices choices = new Choices();
                        choices.setContent(choice.getContent());
                        choices.setIdentifier(choice.getIdentifier());
                        choices.setSeqIndex(strIndex[subItemChoice.size()]);
                        subItemChoice.add(choices);
                    }
                    subItemModel.setChoices(subItemChoice);
                    subItems.add(subItemModel);
                }
                temp.setSubItems(subItems);
                itemTemplate = temp;
                break;
            //组合题
            case COMPOSITE:
                OpenCompositeAssessmentItem openCompositeAssessmentItem = (OpenCompositeAssessmentItem) openAssessmentItem;
                ItemTemplate tempComposite = new ItemTemplate();
                tempComposite.setOriginalValue(openCompositeAssessmentItem.getPrompt().getOriginalValue());
                itemTemplate = tempComposite;
                break;
            //问答题
            case EXTENDEDTEXT:
                List<Integer> list = new ArrayList<>();
                OpenExtendedTextAssessmentItem openExtendedTextAssessmentItem = (OpenExtendedTextAssessmentItem) openAssessmentItem;
                ItemTemplate tempExtendText = new ItemTemplate();
                list.add(openExtendedTextAssessmentItem.getExpectedLength());
                tempExtendText.setOriginalValue(openExtendedTextAssessmentItem.getPrompt().getOriginalValue());
                tempExtendText.setCorrentResponseValue(openExtendedTextAssessmentItem.getCorrentResponseValue());
                tempExtendText.setExpectedLength(list);
                itemTemplate = tempExtendText;
                break;
            //判断题
            case JUDGEMENT:
                OpenJudgementAssessmentItem openJudgementAssessmentItem = (OpenJudgementAssessmentItem) openAssessmentItem;
                ItemTemplate tempJudgement = new ItemTemplate();
                tempJudgement.setOriginalValue(openJudgementAssessmentItem.getPrompt().getOriginalValue());
                tempJudgement.setChoices(new ArrayList<>());
                for (OpenSimpleChoiceAtomicItem choice:openJudgementAssessmentItem.getChoices()) {
                    Choices choices = new Choices();
                    choices.setIdentifier(choice.getIdentifier());
                    choices.setContent(choice.getContent());
                    tempJudgement.getChoices().add(choices);
                }
                itemTemplate = tempJudgement;
                break;
            //匹配题
            case MATCH:
                break;
            //多选题
            case MULTIPLECHOICE:
                OpenMultipleChoiceAssessmentItem openMultipleChoiceAssessmentItem = (OpenMultipleChoiceAssessmentItem) openAssessmentItem;
                ItemTemplate tempMultipleChoice = new ItemTemplate();
                tempMultipleChoice.setOriginalValue(openMultipleChoiceAssessmentItem.getPrompt().getOriginalValue());
                tempMultipleChoice.setChoices(new ArrayList<>());
                for (OpenSimpleChoiceAtomicItem choice:openMultipleChoiceAssessmentItem.getChoices()) {
                    Choices choices = new Choices();
                    choices.setContent(choice.getContent());
                    choices.setIdentifier(choice.getIdentifier());
                    choices.setSeqIndex(strIndex[tempMultipleChoice.getChoices().size()]);
                    tempMultipleChoice.getChoices().add(choices);
                }
                tempMultipleChoice.setMappingResponse(openMultipleChoiceAssessmentItem.isMappingResponse());
                itemTemplate = tempMultipleChoice;
                break;
            //阅读理解题
            case READINGCOMPREHENSION:
                OpenReadingComprehensionAssessmentItem openReadingComprehensionAssessmentItem = (OpenReadingComprehensionAssessmentItem) openAssessmentItem;
                ItemTemplate tempReading = new ItemTemplate();
                tempReading.setOriginalValue(openReadingComprehensionAssessmentItem.getPrompt().getOriginalValue());
                List<ItemTemplate> subItemsReading = new ArrayList<>();
                for (OpenSingleChoiceAssessmentItem sub:openReadingComprehensionAssessmentItem.getSubItems()) {
                    ItemTemplate subItemModel = new ItemTemplate();
                    subItemModel.setOriginalValue(sub.getPrompt().getOriginalValue());
                    List<Choices> subItemChoice = new ArrayList<>();
                    for (OpenSimpleChoiceAtomicItem choice:sub.getChoices()) {
                        Choices choices = new Choices();
                        choices.setContent(choice.getContent());
                        choices.setIdentifier(choice.getIdentifier());
                        choices.setSeqIndex(strIndex[subItemChoice.size()]);
                        subItemChoice.add(choices);
                    }
                    subItemModel.setChoices(subItemChoice);
                    subItemsReading.add(subItemModel);
                }
                tempReading.setSubItems(subItemsReading);
                itemTemplate = tempReading;
                break;
            //单选题
            case SINGLECHOICE:
                OpenSingleChoiceAssessmentItem openSingleChoiceAssessmentItem = (OpenSingleChoiceAssessmentItem) openAssessmentItem;
                ItemTemplate tempSingleChoice = new ItemTemplate();
                tempSingleChoice.setOriginalValue(openSingleChoiceAssessmentItem.getPrompt().getOriginalValue());
                tempSingleChoice.setChoices(new ArrayList<>());
                for (OpenSimpleChoiceAtomicItem choice:openSingleChoiceAssessmentItem.getChoices()) {
                    Choices choices = new Choices();
                    choices.setContent(choice.getContent());
                    choices.setIdentifier(choice.getIdentifier());
                    choices.setSeqIndex(strIndex[tempSingleChoice.getChoices().size()]);
                    tempSingleChoice.getChoices().add(choices);
                }
                itemTemplate = tempSingleChoice;
                break;
            //填空题
            case TEXTENTRY:
                OpenTextEntryAssessmentItem openTextEntryAssessmentItem = (OpenTextEntryAssessmentItem) openAssessmentItem;
                ItemTemplate tempTextEnrty = new ItemTemplate();
                tempTextEnrty.setExpectedLength(openTextEntryAssessmentItem.getTextEntryInteractions().stream().map(OpenTextEntryInteraction::getExpectedLength).collect(Collectors.toList()));
                tempTextEnrty.setItemBodyContent(openTextEntryAssessmentItem.getItemBodyContent());
                tempTextEnrty.setChildItemScore(openTextEntryAssessmentItem.getChildItemScore());
                itemTemplate = tempTextEnrty;
                break;
            //上传题
            case UPLOAD:
                break;
            //排序题
            case ORDER:
                break;
            default:
                break;
        }

        return itemTemplate;
    }

    /**
     * 获取正确答案
     * @param item
     * @return
     */
    public static List<String> getItemResult(OpenAssessmentItem item){
        List<String> result = new ArrayList<>();
        ItemType itemType = item.getAssessmentItemType();
        switch (itemType){
            //单选
            case SINGLECHOICE:
                OpenSingleChoiceAssessmentItem choiceAssessmentItem = (OpenSingleChoiceAssessmentItem) item;
                for (OpenSimpleChoiceAtomicItem choice:choiceAssessmentItem.getChoices()) {
                    if(choice.isCorrectResponse()){
                        result.add(choice.getIdentifier());
                        break;
                    }
                }
                break;
            //多选
            case MULTIPLECHOICE:
                OpenMultipleChoiceAssessmentItem multipleChoiceAssessmentItem = (OpenMultipleChoiceAssessmentItem) item;
                for (OpenSimpleChoiceAtomicItem choice:multipleChoiceAssessmentItem.getChoices()) {
                    if(choice.isCorrectResponse()){
                        result.add(choice.getIdentifier());
                    }
                }
                break;
            //判断
            case JUDGEMENT:
                OpenJudgementAssessmentItem judgementAssessmentItem = (OpenJudgementAssessmentItem) item;
                for (OpenSimpleChoiceAtomicItem choice:judgementAssessmentItem.getChoices()) {
                    if(choice.isCorrectResponse()){
                        result.add(choice.getIdentifier());
                        break;
                    }
                }
                break;
            //填空
            case TEXTENTRY:
                OpenTextEntryAssessmentItem textEntryAssessmentItem = (OpenTextEntryAssessmentItem) item;
                for (OpenTextEntryInteraction t:textEntryAssessmentItem.getTextEntryInteractions()) {
                    for (String corr:t.getCorrectResponseValues().keySet()) {
                        result.add(corr);
                    }
                }
                break;
            //阅读理解
            case READINGCOMPREHENSION:
                OpenReadingComprehensionAssessmentItem readingComprehensionAssessmentItem = (OpenReadingComprehensionAssessmentItem) item;
                for (OpenSingleChoiceAssessmentItem t:readingComprehensionAssessmentItem.getSubItems()) {
                    for (OpenSimpleChoiceAtomicItem choice:t.getChoices()) {
                        if(choice.isCorrectResponse()){
                            result.add(choice.getIdentifier());
                        }
                    }
                }
                break;
            //完形填空
            case CLOZE:
                OpenClozeAssessmentItem clozeAssessmentItem = (OpenClozeAssessmentItem) item;
                for (OpenSingleChoiceAssessmentItem t:clozeAssessmentItem.getSubItems()) {
                    for (OpenSimpleChoiceAtomicItem choice:t.getChoices()) {
                        if(choice.isCorrectResponse()){
                            result.add(choice.getIdentifier());
                        }
                    }
                }
                break;
            default:
                break;
        }
        return result;
    }
}
